package com.lottery.game.core.modules.service.app.v2.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lottery.game.common.exception.BaseException;
import com.lottery.game.common.utils.Encodes;
import com.lottery.game.common.utils.MD5Util;
import com.lottery.game.common.utils.MapUtils;
import com.lottery.game.core.model.dto.UserToken;
import com.lottery.game.core.modules.entity.user.GameUser;
import com.lottery.game.core.modules.service.app.GameUserService;
import com.lottery.game.core.modules.service.app.v2.PassportService;
import com.lottery.game.core.modules.service.app.v2.SignService;
import com.lottery.game.core.modules.service.redis.RedisService;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

import static com.lottery.game.core.modules.service.app.v2.impl.SignServiceImpl.SIGN_KEY;

/**
 * @author : yinlin
 * @version : 1.0
 * @date : 2018/11/9 4:29 PM
 * @Description :
 * @Copyright : Copyright (c) 2018
 * @Company : KeRuYun Technology(Beijing) Chengdu Co. Ltd.
 * @link : com.vpay.exchange.core.base.service.impl.PassportServiceImpl
 */
@Service
public class PassportServiceImpl implements PassportService {

    public static final String LOGIN_KEY = "lottery-login-key";

    private static final String LOGIN_SESSION_KEY = "login-online-session";

    @Autowired
    private GameUserService userService;

    @Autowired
    private SignService signService;

    @Autowired
    private RedisService redisFeignClient;

    @Override
    public String getToken(String openid) {
        return null;
    }

    @Override
    public String getToken(GameUser user) {
        UserToken userToken = UserToken.toUserToken(user);
        userToken.setSign(signService.getSign(MapUtils.toStringTreeMap(userToken), LOGIN_KEY));
        String ticket = Encodes.encodeBase64(JSON.toJSONString(userToken));
        redisFeignClient.set(buildSessionKey(user.getPhone()), ticket);
        return ticket;
    }

    private String buildSessionKey(String phone) {
        return LOGIN_SESSION_KEY + ":" + phone;
    }

    @Override
    public String getToken(String openid, Long ticketTimeOut) {
        return null;
    }

    @Override
    public String login(String username, String password) {
        GameUser user = userService.getGameUserByUserName(username);
        //兼容用户名或者手机号登陆
        if (user == null) {
            throw new BaseException(401, "登陆用户不存在");
        }
        if (!MD5Util.MD532(MD5Util.MD532(password), user.getSalt()).equals(user.getPassword())) {
            throw new BaseException(401, "登陆密码不正确");
        }
        return getToken(user);
    }

    @Override
    public Map checkToken(String token) {
        Map<String, Object> tokenMap;
        try {
            tokenMap = JSONObject.parseObject(new String(Encodes.decodeBase64(token)), Map.class);
        } catch (Exception e) {
            throw new BaseException(401, "登陆验证失败，请重新登陆");
        }
        String ticket = redisFeignClient.get(this.buildSessionKey((String)tokenMap.get("phone")));
        if (ticket == null || !ticket.equals(token)) {
            throw new BaseException(401, "您已在别处登录，请重新登陆");
        }
        return signService.checkSign(MapUtils.map2StringTreeMap(tokenMap), String.valueOf(tokenMap.get(SIGN_KEY)), LOGIN_KEY);
    }
}
